<?php
session_start();
$f='';
if (isset($_GET['f'])){
    $f=$_GET['f'];
}
$zslink='';
if (isset($_GET['zslink'])){
    $zslink=$_GET['zslink'];
}

include './c/config.php';

$access_token = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appId.'&secret='.$secret.'&code='.$_GET['code'].'&grant_type=authorization_code';
$access = file_get_contents($access_token);
$getData = json_decode($access, true);
 
//$url='http://127.0.0.1:8031/api/wx/access_token?appId='.$appId.'&code='.$_GET['code'];
//$getData=json_decode(file_get_contents($url), true);
$openid=$getData['openid'];
if($openid == $f){
   $f='';
}
 
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
$_SESSION['openid']=$openid;
$_SESSION['friendOp']=$f;

if (!empty($zslink) && !empty($openid)) { //统计组数据
$sql="select id from `zsu_user_log` where openid='".$openid."'  limit 0,1 ";
$query = $mysqli->query($sql);
if($query->num_rows>0){

}else{
$sql="INSERT INTO `zsu_user_log` (`id`, `openid`, `zslink`,`ji`, `createtime`) VALUES (NULL, '".$openid."', '".$zslink."','1', '".date("Y-m-d H:i:s")."')";
$mysqli->query($sql);
}
$query=NULL;
}


if (isset($_GET['f']) && !empty($_GET['f']) && $_GET['f'] != $openid){ //好友分享，增加玩次数
         $queryFriend=$mysqli->query("select id from y1_users where openid='".$_GET['f']."' limit 0, 1");
         if($queryFriend->num_rows>0){
               $queryShare=$mysqli->query("select id from y1_share where woid='".$_GET['f']."' and fid='".$openid."' limit 0, 1");
               if($queryShare->num_rows>0){}else{
                
                $mysqli->query("INSERT INTO `y1_share` (`id`, `woid`, `fid`, `createtime`) VALUES (NULL,'".$_GET['f']."','".$openid."','".date("Y-m-d H:i:s")."')");
               }
         }

            $sql="select id from `zsu_user_log` where openid='".$openid."'  limit 0,1 ";
            $query = $mysqli->query($sql);
            if($query->num_rows>0){ }else{
                 $queryLieBian=$mysqli->query("select ji,zslink from zsu_user_log where openid='".$_GET['f']."' limit 0, 1");
                 if($queryLieBian->num_rows>0){
                    $getLieBian=$queryLieBian->fetch_assoc();
                    $ji=3;
                    if(1 == $getLieBian['ji']){
                        $ji=2;
                    }
                    if(2 == $getLieBian['ji']){
                        $ji=3;
                    }
                    $sql="INSERT INTO `zsu_user_log` (`id`, `openid`, `zslink`,`ji`, `createtime`) VALUES (NULL, '".$openid."', '".$getLieBian['zslink']."',".$ji.", '".date("Y-m-d H:i:s")."')";
                    $mysqli->query($sql);

                 }else{
                      $queryG=$mysqli->query("select id from zuansu_user where openid='".$openid."'  limit 0, 1");
                      if($queryG->num_rows>0){
                         $getD=$queryG->fetch_assoc();
                         $uid=$getD['id'];
                         $sql="INSERT INTO `zsu_user_log` (`id`, `openid`, `zslink`,`ji`, `createtime`) VALUES (NULL, '".$openid."', '".$uid."','1', '".date("Y-m-d H:i:s")."')";
                         $mysqli->query($sql);
                      }


                 }  
            }
}

$sql="select id from ".$tableVer."_users where openid='".$openid."'  limit 0,1 ";
$query = $mysqli->query($sql);

if($query->num_rows>0){
    header('Location: http://climb-game.51wom.com/?op='.$openid);
    exit;
}else{
    
    $redirect_uri = urlencode('http://climb-game.51wom.com/c.php?f='.$f);
    $api_userinfo ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appId.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo#wechat_redirect';
    header('Location: '.$api_userinfo);
    exit;
}
 

